//
//  AINavigationController.swift
//  你的AI
//
//  Created by 丸子 on 2024/6/3.
//

import UIKit

class AINavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        let appearance = UIBarButtonItem.appearance() // 获取 UIBarButtonItem 类型的外观对象
        appearance.setBackButtonTitlePositionAdjustment(UIOffset(horizontal: 0.0, vertical: -60), for: .default) // 设置返回按钮标题位置调整
        // 设置导航栏透明
        self.navigationBar.isTranslucent = false
//        self.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationBar.shadowImage = UIImage()
        self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white, NSAttributedString.Key.font: UIFont.pingFangSCMedium(ofSize: 16)] // 设置导航栏标题文本属性
        self.navigationBar.tintColor = UIColor(hexString: "#28272D") // 设置导航栏着色颜色
        if #available(iOS 13.0, *) { // 如果系统版本大于等于 iOS 13，则使用新的外观 API
            let appearancec = UINavigationBarAppearance() // 创建 UINavigationBarAppearance 对象
            appearancec.configureWithTransparentBackground() // 配置外观对象为透明背景
            appearancec.backgroundColor = Theme.color.main
            appearancec.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white, NSAttributedString.Key.font: UIFont.pingFangSCMedium(ofSize: 16)] // 设置外观对象标题文本属性
            self.navigationBar.standardAppearance = appearancec // 设置导航栏标准外观为外观对象
            self.navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance // 设置导航栏滚动边缘外观为标准外观
        } else {
            // Fallback on earlier versions
        }
        self.interactivePopGestureRecognizer?.delegate = self; // 设置交互式弹出手势识别器的代理为当前对象
        self.interactivePopGestureRecognizer?.isEnabled = true; // 启用交互式弹出手势识别器
    }
    override var childForStatusBarStyle: UIViewController? {
        return topViewController
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) { // 重写 pushViewController 方法，用于在推送视图控制器时进行自定义操作
        if self.viewControllers.count > 0 { // 如果当前导航控制器中已经有视图控制器，则执行以下操作
            
            viewController.hidesBottomBarWhenPushed = true // 隐藏底部栏
            
            let btn = UIButton(type: UIButton.ButtonType.custom) // 创建自定义类型的按钮对象
            btn.setBackgroundImage(UIImage(named: "icon_video_back"), for: UIControl.State.normal) // 设置按钮正常状态下的背景图片
            btn.setBackgroundImage(UIImage(named: "icon_video_back"), for: UIControl.State.highlighted) // 设置按钮高亮状态下的背景图片
            btn.frame.size = btn.currentBackgroundImage?.size ?? CGSize.zero // 设置按钮尺寸为当前背景图片的尺寸
            btn.addTarget(self, action: #selector(clickReturn), for: UIControl.Event.touchUpInside) // 给按钮添加点击事件，点击时调用 clickReturn 方法
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn) // 将视图控制器的左侧导航项设置为自定义视图按钮对象
        } else {
            
        }
        
        super.pushViewController(viewController, animated: animated) // 调用父类方法推送视图控制器
    }
    
    // 定义点击返回方法，用于在点击返回按钮时执行弹出视图控制器操作
    @objc func clickReturn() {
        self.popViewController(animated: true) // 弹出当前顶部的视图控制器
    }
}
